<?php
/**
 * 权限
 */
namespace app\model;

use app\controller\HomeBase;
use app\model\Chain;

class CompanyModel extends HomeBase
{
    
   
    /**
    * 企业列表
    */
    public function GetList($field=[],$condition=array(),$page=1,$limit=15,$order=['score'=>'desc']){

    	$searchParams['index'] = config('app.elasticsearch')['CompanyIndex'];
    	$searchParams['type'] = config('app.elasticsearch')['type'];
    	
    	//$searchParams['body']['query']['bool']['must']=[];
    	//$searchParams['body']['query']['bool']['should'][]=array();
    	$must=[];
    	$should=[];
    	
    	if($limit>15&&$limit!=2000){
    		$limit=15;
    	}
    	if(isset($condition['keywords'])&&!empty($condition['keywords'])){
    		$must[]=array('match' => array('company_name' => $condition['keywords']));   		
    	}
    	/***云端产业链*/
    	if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
    		$must[]=array('term' => array('chain_id' => $condition['yun_chain_id']));
    	}
    	/***地方产业链*/
    	$ChainModel=new Chain();
    	if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){
    		$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
    		
    		
    		$ChildrenChainIds=$ChainModel->GetChainChildrenListById($condition['chain_id'],!isset($condition['user_id'])?0:$condition['user_id']);
    		//p($ChildrenChainIds);
    		/* if($chain_id){
    			$searchParams['body']['query']['bool']['must'][]=array('term' => array('chain_id' => $chain_id));
    		} */
    		if(!empty($ChildrenChainIds)){
    			if($chain_id){
    				$should[]=array('term' => array('chain_id' => $chain_id));
    			}
    			foreach ($ChildrenChainIds as $val){
    				$should[]=array('term' => array('chain_id' => $val['chain_id']));
    			}
    		}else{
    			if($chain_id){
    				$must[]=array('term' => array('chain_id' => $chain_id));
    			}
    		}
    		
    	}else{
    		//全部产业链
    		if(isset($condition['user_id'])&&!empty($condition['user_id'])){
    			$ChainModel=new Chain();
    			$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
    			if(!empty($chain_ids)){
    				foreach ($chain_ids as $val){
    					$searchParams['body']['query']['bool']['should'][]=array('term' => array('chain_id' => $val));
    				}
    			}
    		}
    	}
    	
    	if (isset($condition['city_id'])&&!empty($condition['city_id'])) {
    		$city_id = $condition['city_id'];
    		$must[]=array('term' => array('city_id' => $city_id));
    	}
    	if (isset($condition['industry_id'])&&!empty($condition['industry_id'])) {
    		$industry_id = $condition['industry_id'];
    		$must[]=array('term' => array('industry_id' => $industry_id));
    	}
    	if (isset($condition['label_id'])&&!empty($condition['label_id'])) {
    		$label_id = $condition['label_id'];
    		$must[]=array('term' => array('label_id' => $label_id));
    	}
    	
    	if(isset($condition['range'])&&!empty($condition['range'])){
    		foreach ($condition['range'] as $key=>$val){
    			foreach ($val as $key1=>$val1){
    				$must[]=array('range' => array($key =>array($key1=>$val1)));
    				
    			}
    		}
    	}
    	$must[]['bool']['should']=$should;
    	$searchParams['body']['query']['bool']['must']=$must;
    	
    	
    	
    	if($field){
    		$searchParams['body']['_source']=$field;
    	}
    	
    	$start=($page-1)*$limit;
    	
    	$searchParams['from'] = $start;
    	$searchParams['size'] = $limit;
    	
    	
    	
    	if($order){
    		foreach ($order as $key=>$val){
    			$searchParams['body']['sort'][$key]=array('order' => $val);
    		}	
    	}
    	
    	$retDoc = $this->client->search($searchParams);
    	
    	
    	$list=[];
    	
    	if($retDoc['hits']['total']['value']!=0&&$retDoc['hits']['hits']){
    		foreach ($retDoc['hits']['hits'] as $val){
    			$list[]=$val['_source'];
    		}
    		
    	}
    	
    	foreach ($list as &$val){
    		if(isset($val['chain_name'])){
    			$val['chain_name']=implode(',',$val['chain_name']);
    		}else{
    			$val['chain_name']='';
    		}
    		if(isset($val['industry_name'])){
    			$val['industry_name']=implode(',',$val['industry_name']);
    		}else{
    			$val['industry_name']='';
    		}
    		
    		if(isset($val['register_money'])){
    			$val['register_money_cg']=intval($val['register_money']);
    		}
    		
    	}
    
    	return  [
    					'total'=>$retDoc['hits']['total']['value'],
    					'per_page'=>$limit,
    					'current_page'=>$page,
    					'data'=>$list
    	];
    			
    	
    }
    /**
     * 企业总数
     */
    public function GetTotal($condition=array()){
    	
    	$searchParams['index'] = config('app.elasticsearch')['CompanyIndex'];
    	$searchParams['type'] = config('app.elasticsearch')['type'];
    	
    	$must=[];
    	$should=[];
    	$limit = request()->param('limit', 10, 'intval');
    	if($limit>15&&$limit!=200){
    		$limit=15;
    	}
    	if(isset($condition['keywords'])&&!empty($condition['keywords'])){
    		$must[]=array('match' => array('company_name' => $condition['keywords']));
    	}
    	/***云端产业链*/
    	if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
    		$must[]=array('term' => array('chain_id' => $condition['yun_chain_id']));
    	}
    	/***地方产业链*/
    	if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){
    		$ChainModel=new Chain();
    		$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
    		$ChildrenChainIds=$ChainModel->GetChainChildrenListById($condition['chain_id'],!isset($condition['user_id'])?0:$condition['user_id']);
    		//p($ChildrenChainIds);
    		if(!empty($ChildrenChainIds)){
    			if($chain_id){
    				$should[]=array('term' => array('chain_id' => $chain_id));
    			}
    			foreach ($ChildrenChainIds as $val){
    				$should[]=array('term' => array('chain_id' => $val['chain_id']));
    			}
    		}else{
    			if($chain_id){
    				$must[]=array('term' => array('chain_id' => $chain_id));
    			}
    		}
    		
    	}else{
    		//全部产业链
    		if(isset($condition['user_id'])&&!empty($condition['user_id'])){
    			$ChainModel=new Chain();
    			$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
    			if(!empty($chain_ids)){
    				foreach ($chain_ids as $val){
    					$searchParams['body']['query']['bool']['should'][]=array('term' => array('chain_id' => $val));
    				}
    			}
    		}
    	}
    	
    	if (isset($condition['city_id'])&&!empty($condition['city_id'])) {
    		$city_id = $condition['city_id'];
    		$must[]=array('term' => array('city_id' => $city_id));
    	}
    	if (isset($condition['industry_id'])&&!empty($condition['industry_id'])) {
    		$industry_id = $condition['industry_id'];
    		$must[]=array('term' => array('industry_id' => $industry_id));
    	}
    	if (isset($condition['label_id'])&&!empty($condition['label_id'])) {
    		$label_id = $condition['label_id'];
    		$must[]=array('term' => array('label_id' => $label_id));
    	}
    	if(isset($condition['range'])&&!empty($condition['range'])){
    		foreach ($condition['range'] as $key=>$val){ 			
    			foreach ($val as $key1=>$val1){
    				$must[]=array('range' => array($key =>array($key1=>$val1)));
    			}
    		}
    	}
    	$must[]['bool']['should']=$should;
    	$searchParams['body']['query']['bool']['must']=$must;
    	$retDoc = $this->client->count($searchParams);
    	return $retDoc['count'];
    	
    }
    /**
     * 企业详情
     */
    public function GetDetail($identification=''){
    	if(empty($identification)){
    		return [];
    	}
    	$searchParams['index'] = config('app.elasticsearch')['CompanyIndex'];
    	$searchParams['type'] = config('app.elasticsearch')['type'];
    	
    	$searchParams['body']['query']['bool']['must']=[];
    	$searchParams['body']['query']['bool']['should']=[];
    	
    	$searchParams['body']['query']['bool']['must'][]=array('match_phrase' => array('identification' => $identification));
    	
    	$retDoc = $this->client->search($searchParams);
    	
    	$info=[];
    	
    	if($retDoc['hits']['total']['value']!=0&&$retDoc['hits']['hits']){
    		foreach ($retDoc['hits']['hits'] as $val){
    			$info=$val['_source'];
    			break;
    		}
    		
    	}  	
    	return $info;	
    }
    /**
     * 通过name获取企业id
     */
    public function GetCompanyIdentificationByName($company_name=''){
    	if(empty($company_name)){
    		return '';
    	}
    	$searchParams['index'] = config('app.elasticsearch')['CompanyIndex'];
    	$searchParams['type'] = config('app.elasticsearch')['type'];
    	
    	$searchParams['body']['query']['bool']['must']=[];
    	$searchParams['body']['query']['bool']['should']=[];
    	
    	$searchParams['body']['query']['bool']['must'][]=array('match_phrase' => array('company_name' => $company_name));
    	
    	$retDoc = $this->client->search($searchParams);
    	
    	$info=[];
    	
    	if($retDoc['hits']['total']['value']!=0&&$retDoc['hits']['hits']){
    		foreach ($retDoc['hits']['hits'] as $val){
    			$info=$val['_source'];
    			break;
    		}
    		
    	}
    	if(!empty($info)){
    		return $info['identification'];
    	}else{
    		return '';
    	}
    }
    /**
     * 通过id获取企业name
     */
    public function GetCompanyNameByIdentification($Identification=''){
    	if(empty($Identification)){
    		return '';
    	}
    	//p($Identification);
    	$searchParams['index'] = config('app.elasticsearch')['CompanyIndex'];
    	$searchParams['type'] = config('app.elasticsearch')['type'];
    	
    	$searchParams['body']['query']['bool']['must']=[];
    	$searchParams['body']['query']['bool']['should']=[];
    	
    	$searchParams['body']['query']['bool']['must'][]=array('match_phrase' => array('identification' => $Identification));
    	
    	$retDoc = $this->client->search($searchParams);
    	
    	$info=[];
    	
    	if($retDoc['hits']['total']['value']!=0&&$retDoc['hits']['hits']){
    		foreach ($retDoc['hits']['hits'] as $val){
    			$info=$val['_source'];
    			break;
    		}
    		
    	}
    	if(!empty($info)){
    		return $info['company_name'];
    	}else{
    		return '';
    	}
    }

}
 